package com.ctshk.sapp.finance.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.rpc.finance.dto.bill.BillGroupRecordExportDTO;
import com.ctshk.rpc.finance.req.bill.BillGroupRecordExportReq;
import com.ctshk.rpc.finance.service.IFinanceBillGroupRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.ctshk.common.web.support.BaseController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 团期认款记录 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-06-22
 */
@RestController
@Api(value = "团期认款记录", tags = "团期认款记录")
@RequestMapping("/finance-bill-group-record")
public class FinanceBillGroupRecordController extends BaseController {

    @DubboReference
    private IFinanceBillGroupRecordService service;


    @ApiOperation(value = "导出", notes = "导出团期认款记录（叶冠峰）")
    @OperationLogAnnotation(operModul = OperationLogModel.FINANCE, operType = OperationLogType.QUERY, operTitle = "导出团期认款记录", operDesc = "导出团期认款记录")
    @PostMapping("/export")
    public void export(@RequestBody @Valid BillGroupRecordExportReq req){
        List<List<String>> list = service.export(req);
        if (CollectionUtils.isEmpty(list)){
            throw new BusinessException(SystemError.FINANCE_ERROR_17041);
        }
        HttpServletResponse response = getResponse();
        response.setContentType("text/plain");
        String fileName = DateUtil.fmtLocalDate(LocalDate.now(),"yyyyMMdd");
        response.setHeader("Content-Disposition","attachment; filename=" + fileName + ".txt");
        BufferedOutputStream buff = null;
        StringBuffer write = new StringBuffer();
        String enter = "\r\n";
        String end = ";";
        ServletOutputStream outSTr = null;
        try {
            outSTr = response.getOutputStream();
            buff = new BufferedOutputStream(outSTr);
            for (List<String> data : list) {
                for (String str : data) {
                    write.append(str + end);
                }
                write.append(enter);
            }
            buff.write(write.toString().getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            throw new BusinessException(SystemError.SYS_408);
        } finally {
            try {
                buff.close();
                outSTr.close();
            } catch (Exception e) {
                throw new BusinessException(SystemError.SYS_408);
            }
        }
    }


}
